package com.example.controller;

import com.example.anno.MyLog;
import com.example.pojo.Emp;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    //条件分页查询
    @GetMapping
    //设置参数的默认值
    public Result page(@RequestParam(defaultValue = "1") Integer page ,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){


        log.info("请求参数为：{},{},{},{},{}",page,pageSize,name,gender,begin,end);

        PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);

        return Result.success(pageBean);
    }

    @MyLog
    //批量删除员工
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable ArrayList<Integer> ids){
        log.info("请求参数为:{}",ids);

        empService.delete(ids);

        return Result.success();
    }

    @MyLog
    //新增员工
    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("请求参数为：{}",emp);

        empService.add(emp);

        return Result.success();
    }

    //根据Id查询员工
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("请求参数为：{}",id);

        Emp emp =  empService.selectById(id);

        return Result.success(emp);
    }

    @MyLog
    //修改员工
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("请求参数为：{}",emp);

        empService.update(emp);

        return Result.success();
    }

}
